package trs.example;

import java.util.Arrays;

import cn.edu.njust.alg.ArrayUtils;

public class BinarySearch {

	public static int binsearch(int[] array, int x) {

		int low = 0;
		int high = array.length - 1;
		int mid;

		while (low <= high) {
			mid = (low + high) / 2;
			if (array[mid] == x) {
				return mid;
			} else if (array[mid] > x) {
				high = mid - 1;
			} else {
				low = mid + 1;
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] array = { 23, 5, 67, 8, 9, 6, 7, 85, 34, 354 };
		Arrays.sort(array);
		// {5, 6, 7, 8, 9, 23, 34, 67, 85, 354}
		ArrayUtils.printArray(array);
		System.out.println(binsearch(array, 100));

	}

}
